package org.sample.dining.order;

import org.sample.dining.AbstractChopstick;
import org.sample.dining.AbstractPhilosopher;

/**
 * Created by alan on 2017/7/12.
 */
public class Philosopher extends AbstractPhilosopher {

    public Philosopher(String name, AbstractChopstick left, AbstractChopstick right) {
        super(name, left, right);
    }

    @Override
    public boolean useChopsticks() {
        int leftIndex = this.left.getIndex();
        int rightIndex = this.right.getIndex();

        AbstractChopstick left = this.left;
        AbstractChopstick right = this.right;

        if (leftIndex > rightIndex) {
            left = this.right;
            right = this.left;
        }

        left.take();
        right.take();
        System.out.println(name + " 使用筷子" + leftIndex + "," + rightIndex);
        right.drop();
        left.drop();
        return true;
    }
}